/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.seari.pa.handler;

import java.util.concurrent.TimeUnit;

import org.slf4j.LoggerFactory;

import com.seari.ats.AtsClient;
import com.seari.bean.pa.PaResponse;
import com.seari.pa.PaClient;

/**
 *
 * @author Rainbow
 */
public class PaTimer implements Runnable
{
    //终止线程
    private boolean terminate = false;
    // 清除
    private boolean reset = false;
    // 发送时间
    private volatile long next_send_time;
    //客户端
    private PaClient client;
    //日志
    protected static org.slf4j.Logger logger = LoggerFactory.getLogger(PaTimer.class);
    @Override
    public void run()
    {
    	
        while (!terminate)
        { 
        	
        	
            try
            {
                if (!reset)
                {
                    logger.debug("timer is not actived");
                    TimeUnit.SECONDS.sleep(10);
                    continue;
                }
                //当前时间
                long current = System.currentTimeMillis();
                // 剩余时间
                long timeLeft = next_send_time - current;
                logger.debug("time left:" + timeLeft);
                
                if(timeLeft > 0)
                {
                    //do nothing
                }else{
                    PaResponse paResponse = new PaResponse();
                    paResponse.stationNum = 0;
                    paResponse.paNum = (byte) 0xFF;
                    paResponse.chk = (byte) (0x90 + 0x31 + paResponse.stationNum + paResponse.paNum);
                    reset = false;
                }
                TimeUnit.SECONDS.sleep(10);
            } catch (InterruptedException ex)
            {
                logger.warn(ex.getMessage());
            }
        }
    }
    
    public void refreshTimer()
    {
        this.reset = true;
        this.next_send_time = System.currentTimeMillis() + 90 * 1000;
    }

    public boolean getTerminate()
    {
        return terminate;
    }

    public void setTerminate(boolean terminate)
    {
        this.terminate = terminate;
    }

    public boolean getReset()
    {
        return reset;
    }

    public void setReset(boolean reset)
    {
        this.reset = reset;
    }

    public PaClient getClient()
    {
        return client;
    }

    public void setClient(PaClient client)
    {
        this.client = client;
    }
    
    
}
